<?php
require_once '../entity/Sign.php';
require_once '../entity/Masker.php';
require_once '../entity/Watcher.php';
require_once 'NotificationDao.php';
require_once 'DaoUtils.php';
class SignDao extends DaoUtils {
	public function insertSign($sign) {
		$sign -> text = $this -> escape($sign -> text);
		
		if (!isset($sign-> id_sign_father)) {
			$sign-> id_sign_father = "null";
		} else {
			$notificationDao = new NotificationDao();
			$signFather = $this->getSign($sign-> id_sign_father);
			$notificationDao->insertNotificationWithText($sign -> id_masker, $signFather -> id_masker, NotificationType::$signReply, $sign -> text);
		}
		
		$query = "insert into " . Sign::getTableName() . " (id_masker, id_sign_father, text) values (" . $sign -> id_masker . "," . $sign -> id_sign_father . ",'" . $sign -> text . "')";
		$result = $this -> insert($query);

		return $result;
	}
	
	public function insertColorfeelChoosed($signId, $color, $idMaskerFrom) {
		$signId = $this -> escape($signId);
		$color = $this -> escape($color);
		
		if ($color != "r" && $color != "g" && $color != "b") {
			return false;
		}
		
		$query = "update " . Sign::getTableName() . " SET colorfeel_" . $color . " = colorfeel_" . $color . " + 1 where id = " . $signId;

		$result = $this -> update($query);
		
		$notificationDao = new NotificationDao();
		$sign = $this->getSign($signId);
		$notificationDao->insertNotification($idMaskerFrom, $sign -> id_masker, NotificationType::$signColorfeel);
		
		return $result;
	}
	
	public function deleteSign($signId, $idMasker) {
		$signId = $this -> escape($signId);
		
		$query = "delete from " . Sign::getTableName() . " WHERE id = " . $signId . " and id_masker = " . $idMasker;

		$result = $this -> delete($query);

		return $result;
	}

	public function getGlobalSignList($limitFrom, $limitTo, $lastId) {
		return $this ->getSignList($limitFrom, $limitTo, $lastId, "");
	}
	
	public function getMineSignList($limitFrom, $limitTo, $id_masker, $lastId) {
		return $this ->getSignList($limitFrom, $limitTo, $lastId, " and id_masker = ".$id_masker." ");
	}
	
	public function getWachersSignList($limitFrom, $limitTo, $id_masker, $lastId) {
		return $this ->getSignList($limitFrom, $limitTo, $lastId, " and id_masker IN ( select id_masker_to FROM ".Watcher::getTableName()." WHERE id_masker_from = ".$id_masker.") ");
	}
	
	public function getSignList($limitFrom, $limitTo, $lastId, $where) {
		if(isset($lastId) && $lastId != "") {
			$where .= " and a.id < ".$lastId;
		}
		$totalReplies = "(select COUNT(*) from " . Sign::getTableName() . " c where a.id = c.id_sign_father) as total_replies";
		$query = "select ".$totalReplies.", a.id, a.insert_date, a.text, a.id_masker, b.name as name_masker, a.id_sign_father, a.colorfeel_r, a.colorfeel_g, a.colorfeel_b  from " . Sign::getTableName() . " a join " . Masker::getTableName() . " b on a.id_masker = b.id where 1=1 ".$where." order by id desc LIMIT ".$limitFrom.", ".$limitTo." ";
		
		$records = $this -> selectRecords($query);
		$signArray = array();
		foreach ($records as $record)  {
			$sign = new Sign();
			$sign->setSign($record['id'], $record['id_masker'], $record['id_sign_father'], $record['text'], 
				$record['insert_date'], $record['name_masker'], $record['colorfeel_r'], $record['colorfeel_g'], 
				$record['colorfeel_b'],$record['total_replies']);
			array_push($signArray,$sign);
		} 
		return $signArray;
	}

	public function getSign($id) {
		$totalReplies = "(select COUNT(*) from " . Sign::getTableName() . " c where a.id = c.id_sign_father) as total_replies";
		$query = " select ".$totalReplies.", a.id, a.insert_date, a.text, a.id_masker, b.name as name_masker, a.id_sign_father, a.colorfeel_r, a.colorfeel_g, a.colorfeel_b  ";
		$query .= " from " . Sign::getTableName() . " a join " . Masker::getTableName() . " b on a.id_masker = b.id ";
		$query .= " where a.id = ".$id." order by a.id desc ";
		
		$record = $this -> selectRecord($query);
		
		$sign = new Sign();
		if($record != null) {
			$sign->setSign($record['id'], $record['id_masker'], $record['id_sign_father'], $record['text'], 
				$record['insert_date'], $record['name_masker'], $record['colorfeel_r'], $record['colorfeel_g'], 
				$record['colorfeel_b'],$record['total_replies']);
		} 
		
		return $sign;
	}

	public function getRepliesSignList($limitFrom, $limitTo, $signIdFather, $lastId) {
		return $this ->getSignList($limitFrom, $limitTo, $lastId, " and a.id_sign_father = ".$signIdFather." ");
	}

}
?>